package com.imooc.homework;

import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class PlanReader_dom4j {
	public void readXml() {
		//读取文件
		String file = "D:\\pro_software\\eclipse-workspace\\xml\\src\\com\\imooc\\homework\\work_1_plan.xml";
		//SAXReader类是读取XML文件的核心类，用于将XML解析后以树的形式保存到内存中
		SAXReader reader = new SAXReader();	
		try {
			Document document = reader.read(file);
			//获取XML文档的根节点，即hr标签
			Element root = document.getRootElement();
			//elements方法用于获取course标签集合
			List<Element> courses  = root.elements("course");
			//for循环遍历course标签集合
			for (Element course : courses) {
				//attribute获得节点属性
				Attribute att = course.attribute("id");
				System.out.println(att.getText());
				System.out.println(course.element("course-name").getText());
				System.out.println(course.element("class-hour").getText());
				System.out.println(course.element("exam-form").getText());			
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
	}
	
	
	public static void main(String[] args) {
		PlanReader_dom4j plan = new PlanReader_dom4j();
		plan.readXml();
	}
}
